#include "cangku.h"
#include "ui_cangku.h"
#include "guoshu.h"
#include "value.h"
#include "admin_0.h"
#include "dlcw.h"
#include "cangku_add.h"
#include <string.h>
#include <QDate>
#include <QDateTime>
#include <QTimer>
#include <QMessageBox>
#include <QStandardItem>

extern int count1;
extern int count2;
extern int count3;

CangKu::CangKu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CangKu)
{
    ui->setupUi(this);
    themodel=new QStandardItemModel(3,3,this);
    ui->tableView->setModel(themodel);
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(TimeUpdate()));
    timer->start(1000); // 每次触发timeout信号时间间隔为1秒
    setWindowTitle("仓库");
    QDateTime cur_time=QDateTime::currentDateTime();
    ui->yearbox->setValue(cur_time.toString("yy").toInt());
    ui->monthbox->setValue(cur_time.toString("MM").toInt());
    ui->daybox->setValue(cur_time.toString("dd").toInt());
}

CangKu::~CangKu()
{
    delete ui;
}

void CangKu::TimeUpdate()
{
    QDateTime atime=QDateTime::currentDateTime();
    QString tt=atime.toString("yyyy年MM月dd日 hh:mm:ss");
    ui->Timelabel->setText(tt);
}

void CangKu::on_action_triggered() //返回主菜单
{
    this->close();
    Admin_0 *a=new Admin_0(this);
    a->show();
}

void CangKu::on_add_triggered()//添加
{
    //判断日期是否合法
    QDateTime ccc=QDateTime::currentDateTime();
    int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if ( ui->yearbox->value() < 0 ||ui->yearbox->value()>ccc.toString("yy").toInt())
    {
        QMessageBox::warning(this,"err","请检查生产日期");
        return;
    }
    if ( ui->monthbox->value() < 1 || ui->monthbox->value()> ccc.toString("MM").toInt() )
    {
        QMessageBox::warning(this,"err","请检查生产日期");
        return;
    }
    if ( ui->monthbox->value() == 2 )
    {
            if (  ui->yearbox->value() % 4 == 0 )
            {
                    monthDays[1] = 29;
            }
    }
    if ( ui->daybox->value() < 1 || ui->daybox->value() > monthDays[ui->monthbox->value()-1]||ui->daybox->value()>ccc.toString("dd").toInt() )
    {
        QMessageBox::warning(this,"err","请检查生产日期");
        return;
    }
    cangku_add *dlg=new cangku_add(this);
    if(dlg->exec()==QDialog::Rejected){
        return;
    }
    std::string serch;
    serch=ui->ordernum->text().trimmed().toStdString();
    QDateTime ti=QDateTime::currentDateTime();
    if(p!=NULL){
        int flag=1;
        if(ui->goodname->text().trimmed().size()==0||ui->goodname->text().trimmed().size()>=20){
            QMessageBox::warning(this,"err","品名长度不合法，请输入20个字以内的品名");
            flag=0;
        }
        else if(ui->nickname->text().trimmed().size()>=20){
            QMessageBox::warning(this,"err","别名长度不合法，请输入20个字以内的品名");
            flag=0;
        }
        else if(ui->numBox->value()<0.1){
            QMessageBox::warning(this,"err","请输入正确的数量");
            flag=0;
        }
        else if(ui->inmoeyBox->value()<0.01){
            QMessageBox::warning(this,"err","请输入正确的进价");
            flag=0;
        }
        else if(1.25*ui->inmoeyBox->value()>=ui->outmoneyBox->value()){
            QMessageBox::warning(this,"err","进价不得低于售价的80%");
            flag=0;
        }
        else if(ui->exp->value()==0){
            QMessageBox::warning(this,"err","请输入正确的保质期");
            flag=0;
        }
        else if(ui->ordernum->text().trimmed().size()==0||ui->ordernum->text().trimmed().size()>=20){
            QMessageBox::warning(this,"err","产品编号长度不合法,请输入20字以内的产品编号");
            flag=0;
        }
        else if(ui->exp->value()==0||judgegq()==0){
            QMessageBox::warning(this,"err","请输入正确的保质期");
            flag=0;
        }
            struct good *tmp;
            tmp=p;
            for(int i=0;i<count1;i++){
                if(strcmp(tmp->number,serch.c_str())==0){
                    QMessageBox::warning(this,"err","产品编号不能重复");
                    flag=0;break;
                }
                tmp=tmp->next;
            }
            QString str=ui->ordernum->text().trimmed();
            int nCount = str.count();
            for(int i = 0 ; i < nCount ; i++)
            {
                QChar cha = str.at(i);
                ushort uni = cha.unicode();
                if(uni >= 0x4E00 && uni <= 0x9FA5)
                {
                    QMessageBox::warning(this,"err","产品编号中不能有汉字");
                    flag=0;break;
                }
            }
            if(flag==0) return;
            tmp=p;
            for(int i=1;i<count1;i++){
                tmp=tmp->next;
            }
            struct good *p1;
            p1=(struct good*)malloc(sizeof(struct good));
            tmp->next=p1;
            p1->next=NULL;
            //printf("果蔬名：");
            //scanf("%s",p1->name);
            std::string tp;
            tp=ui->goodname->text().trimmed().toStdString();
            strcpy(p1->name,tp.c_str());
            //printf("别名：");
            //scanf("%s",p1->nickname);
            tp=ui->nickname->text().trimmed().toStdString();
            if(strlen(tp.c_str())==0){
                tp=" ";
            }
            strcpy(p1->nickname,tp.c_str());
            //printf("编号：");
            //scanf("%s",p1->number);
            tp=ui->ordernum->text().trimmed().toStdString();
            strcpy(p1->number,tp.c_str());
            //printf("数  量：");
            //scanf("%lf",&p1->count);
            double t;
            t=ui->numBox->value();
            if(ui->selltype->currentIndex()==0){        //盒装
               p1->count=int(ui->numBox->value());
               p1->typ=0;
            }
            else{                                       //散装
                p1->count=t;
                p1->typ=1;
            }
            //printf("进  价：");
            //scanf("%lf",&p1->inprice);
            t=ui->inmoeyBox->value();
            p1->inprice=t;
            //printf("售  价：");
            //scanf("%lf",&p1->outprice);
            t=ui->outmoneyBox->value();
            p1->outprice=t;
            //printf("生产日期：");
            //scanf("%s",p1->indate);
            //tp=ui->yearbox->text().toStdString();
            tp=QString::asprintf("%.2d-%.2d-%.2d",ui->yearbox->value(),ui->monthbox->value(),ui->daybox->value()).toStdString();
            strcpy(p1->purchasedate,tp.c_str());
            //tp="-";
            //strcat(p1->purchasedate,tp.c_str());
            //tp=ui->monthbox->text().toStdString();

            //strcat(p1->purchasedate,tp.c_str());
            //tp="-";
            //strcat(p1->purchasedate,tp.c_str());

            //tp=ui->daybox->text().toStdString();
            //strcat(p1->purchasedate,tp.c_str());
            //进货日期
            strcpy(p1->indate,ti.toString("yy-MM-dd hh:mm:ss").toStdString().c_str());
            //printf("保质期：");
            //scanf("%d",&p1->exp);
            int tt=ui->exp->value();
            p1->exp=tt;
            strcpy(name1,p1->name);
            strcpy(nickname1,p1->nickname);
            strcpy(number1,p1->number);
            count01=p1->count;
            inprice1=p1->inprice;
            outprice1=p1->outprice;
            strcpy(indate1,p1->indate);
            strcpy(purchasedate1,p1->purchasedate);
            exp1=p1->exp;
            typ1=p1->typ;

            count1++;
            FILE *w=fopen("shiyan4.txt","w");
            if(w==NULL){
                QMessageBox::warning(this,"err","文件打开失败");
                return;
            }
            int nx=count1;
            struct good *pp;
            pp=p;
            fwrite(&nx,sizeof(int),1,w);
            for(int i=0;i<count1;i++){
                fwrite(pp,sizeof(struct good),1,w);
                pp=pp->next;
            }
            fclose(w);
            cmp_count(p,&count1);
        }else{
        int flag=1;
            if(ui->goodname->text().trimmed().size()==0||ui->goodname->text().trimmed().size()>=20){
                QMessageBox::warning(this,"err","品名长度不合法，请输入20个字以内的品名");
                flag=0;
            }
            else if(ui->nickname->text().trimmed().size()>=20){
                QMessageBox::warning(this,"err","别名长度不合法，请输入20个字以内的品名");
                flag=0;
            }
            else if(ui->numBox->value()<0.1){
                QMessageBox::warning(this,"err","请输入正确的数量");
                flag=0;
            }
            else if(ui->inmoeyBox->value()<0.01){
                QMessageBox::warning(this,"err","请输入正确的进价");
                flag=0;
            }
            else if(1.25*ui->inmoeyBox->value()>=ui->outmoneyBox->value()){
                QMessageBox::warning(this,"err","进价不得低于售价的80%");
                flag=0;
            }
            else if(ui->exp->value()==0||judgegq()==0){
                QMessageBox::warning(this,"err","请输入正确的保质期");
                flag=0;
            }
            else if(ui->ordernum->text().trimmed().size()==0||ui->ordernum->text().trimmed().size()>=20){
                QMessageBox::warning(this,"err","产品编号长度不合法,请输入20字以内的产品编号");
                flag=0;
            }
            else if(ui->exp->value()==0){
                QMessageBox::warning(this,"err","请输入正确的保质期");
                flag=0;
            }
            struct good *tmp,*p1;
            QString str=ui->ordernum->text().trimmed();
            int nCount = str.count();
            for(int i = 0 ; i < nCount ; i++)
            {
                QChar cha = str.at(i);
                ushort uni = cha.unicode();
                if(uni >= 0x4E00 && uni <= 0x9FA5)
                {
                    QMessageBox::warning(this,"err","产品编号中不能有汉字");
                    flag=0;
                    break;
                }
            }
            if(flag==0) return;
            tmp=NULL;
            tmp=(struct good*)malloc(sizeof(struct good));
            p=(struct good*)malloc(sizeof(struct good));
            p->next=tmp;
            p1=tmp;
            //printf("果蔬名：");
            //scanf("%s",p1->name);
            std::string tp;
            tp=ui->goodname->text().trimmed().toStdString();
            strcpy(p1->name,tp.c_str());
            tp=ui->nickname->text().trimmed().toStdString();
            //printf("别名：");
            //scanf("%s",p1->nickname);
            if(strlen(tp.c_str())==0){
                tp=" ";
            }
            strcpy(p1->nickname,tp.c_str());
            //printf("编号：");
            //scanf("%s",p1->number);
            tp=ui->ordernum->text().trimmed().toStdString();
            strcpy(p1->number,tp.c_str());
            //printf("数  量：");
            //scanf("%lf",&p1->count);
            double t;
            t=ui->numBox->value();
            if(ui->selltype->currentIndex()==0){        //盒装
               p1->count=int(ui->numBox->value());
               p1->typ=0;
            }
            else{                                       //散装
                p1->count=t;
                p1->typ=1;
            }
            //printf("进  价：");
            //scanf("%lf",&p1->inprice);
            t=ui->inmoeyBox->value();
            p1->inprice=t;
            //printf("售  价：");
            //scanf("%lf",&p1->outprice);
            t=ui->outmoneyBox->value();
            p1->outprice=t;
            //printf("生产日期：");
            //scanf("%s",p1->indate);
            //tp=ui->yearbox->text().toStdString();
            tp=QString::asprintf("%.2d-%.2d-%.2d",ui->yearbox->value(),ui->monthbox->value(),ui->daybox->value()).toStdString();
            strcpy(p1->purchasedate,tp.c_str());
            //tp="-";
            //strcat(p1->purchasedate,tp.c_str());
            //tp=ui->monthbox->text().toStdString();

            //strcat(p1->purchasedate,tp.c_str());
            //tp="-";
            //strcat(p1->purchasedate,tp.c_str());

            //tp=ui->daybox->text().toStdString();
            //strcat(p1->purchasedate,tp.c_str());
            //printf("保质期：");
            //scanf("%d",&p1->exp);
            int tt=ui->exp->value();
            p1->exp=tt;
            //进货日期
            strcpy(p1->indate,ti.toString("yy-MM-dd hh:mm:ss").toStdString().c_str());
            strcpy(name1,p1->name);
            strcpy(nickname1,p1->nickname);
            strcpy(number1,p1->number);
            count01=p1->count;
            inprice1=p1->inprice;
            outprice1=p1->outprice;
            strcpy(indate1,p1->indate);
            strcpy(purchasedate1,p1->purchasedate);
            exp1=p1->exp;
            typ1=p1->typ;
            p=p->next;
            p->next=NULL;
            count1++;
            FILE *w=fopen("shiyan4.txt","w");
            if(w==NULL){
                return;QMessageBox::warning(this,"err","文件打开失败");
            }
            int nx=count1;
            struct good *pp;
            pp=p;
            fwrite(&nx,sizeof(int),1,w);
            for(int i=0;i<count1;i++){
                fwrite(pp,sizeof(struct good),1,w);
                pp=pp->next;
            }
            fclose(w);
        }
    if(ss1!=NULL){
            struct good *tmp;
            tmp=ss1;
            for(int i=1;i<count2;i++){
                tmp=tmp->next;
            }
            struct good *p1;
            p1=(struct good*)malloc(sizeof(struct good));
            tmp->next=p1;
            p1->next=NULL;
            strcpy(p1->name,name1);
            strcpy(p1->nickname,nickname1);
            strcpy(p1->number,number1);
            p1->count=count01;
            p1->inprice=inprice1;
            p1->outprice=outprice1;
            strcpy(p1->indate,indate1);
            strcpy(p1->purchasedate,purchasedate1);
            p1->exp=exp1;
            p1->typ=typ1;
            count2+=1;
            FILE *w=fopen("shiyan5.txt","w");
            if(w==NULL){
                return;QMessageBox::warning(this,"err","文件打开失败");
            }
            int nx=count2;
            struct good *pp;
            pp=ss1;
            fwrite(&nx,sizeof(int),1,w);
            for(int i=0;i<count2;i++){
                fwrite(pp,sizeof(struct good),1,w);
                pp=pp->next;
            }
            fclose(w);
            cmp_count(ss1,&count2);
        }else{
            struct good *tmp;
            tmp=NULL;
            tmp=(struct good*)malloc(sizeof(struct good));
            ss1=(struct good*)malloc(sizeof(struct good));
            ss1->next=tmp;
            strcpy(tmp->name,name1);
            strcpy(tmp->nickname,nickname1);
            strcpy(tmp->number,number1);
            tmp->count=count01;
            tmp->inprice=inprice1;
            tmp->outprice=outprice1;
            strcpy(tmp->indate,indate1);
            strcpy(tmp->purchasedate,purchasedate1);
            tmp->exp=exp1;
            tmp->typ=typ1;
            count2+=1;
            ss1=ss1->next;
            ss1->next=NULL;
            FILE *w=fopen("shiyan5.txt","w");
            if(w==NULL){
                return;QMessageBox::warning(this,"err","文件打开失败");
            }
            int nx=count2;
            struct good *pp;
            pp=ss1;
            fwrite(&nx,sizeof(int),1,w);
            for(int i=0;i<count2;i++){
                fwrite(pp,sizeof(struct good),1,w);
                pp=pp->next;
            }
            fclose(w);
        }
    QString tptp;
    std::string rea=ui->goodname->text().trimmed().toStdString();
    if(ui->selltype->currentIndex()==0) tptp=QString::asprintf("%s(盒装)",rea.c_str());
    else tptp=QString::asprintf("%s(散装)",rea.c_str());
    rea=tptp.toStdString();
    //strcat(rr.c_str(),rea.c_str());
    IAEP xp;
    xp = (IAEP)malloc(M);        //申请一段大小为M 的空间
    xp->iae=(ui->numBox->value())*(ui->inmoeyBox->value());
    //printf("请输入收支类型（-代表支出，+代表收入）：");
    //scanf("%c", &p->ch);
    xp->ch='-';
    //printf("请输入时间（年 月 日）：");
    //scanf("%d%d%d", &p->year, &p->month, &p->day);
    xp->year=ti.toString("yy").toInt();
    xp->month=ti.toString("MM").toInt();
    xp->day=ti.toString("dd").toInt();
    //printf("请输入收入来源或支出目的：");
    //gets(p->source_or_aim);
    strcpy(xp->source_or_aim,rea.c_str());
    //保存p到文件中
    FILE* fp;       //文件指针
    fp = fopen("admin123", "ab");    //以追加的方式打开文件
    if (fp == NULL)
    {
        //printf("cannot open file\n");
        QMessageBox::warning(this,"err","打开文件失败！");
        fclose(fp);//关闭文件
    }
    else if (fwrite(xp, M, 1, fp) != 1)    //将p所指向的1个大小为M的数据结构写入fp所指向的文件中
    {
        //printf("file write error\n");
        QMessageBox::warning(this,"err","文件写入失败！");
        fclose(fp);//关闭文件
    }
    else {
        fclose(fp);//关闭文件
    }
    xp = (IAEP)malloc(M);        //申请一段大小为M 的空间
    xp->iae=(ui->numBox->value())*(ui->inmoeyBox->value());
    xp->ch='-';
    xp->year=ti.toString("yy").toInt();
    xp->month=ti.toString("MM").toInt();
    xp->day=ti.toString("dd").toInt();
    strcpy(xp->source_or_aim,rea.c_str());
    fp = fopen("admin123_purchase", "ab");    //以追加的方式打开文件
    if (fp == NULL)
    {
        //printf("cannot open file\n");
        QMessageBox::warning(this,"err","打开文件失败！");
        fclose(fp);//关闭文件
    }
    else if (fwrite(xp, M, 1, fp) != 1)    //将p所指向的1个大小为M的数据结构写入fp所指向的文件中
    {
        //printf("file write error\n");
        QMessageBox::warning(this,"err","文件写入失败！");
        fclose(fp);//关闭文件
    }
    else {
        fclose(fp);//关闭文件
    }
    ui->goodname->clear();
    ui->nickname->clear();
    ui->ordernum->clear();
    ui->numBox->setValue(0.00);
    ui->outmoneyBox->setValue(0.00);
    ui->inmoeyBox->setValue(0.00);
    ui->exp->setValue(0);

}

void CangKu::on_view_triggered()//查看历史订单
{
    QStringList headlist;
    headlist<<"进货时间"<<"支出金额"<<"进货产品";
    themodel=new QStandardItemModel(js()+2,3,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    FILE* fp;              //文件指针
    int n = 0;
    int i = 12;
    IAEP head = NULL;
    IAEP mp, p2, pr = NULL;
    fp = fopen("admin123_purchase", "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       // printf("cannot open file\n");
        QMessageBox::warning(this,"err","文件打开失败");
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        n++;
        mp = (IAEP)malloc(M);   //向内存申请一段空间
        fread(mp, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
        if (n == 1)
        {
            head = mp;
            p2 = mp;
            head->pre = NULL;
        }
        else                //创建双向链表
        {
            pr = p2;          //pr是p的前驱结点
            mp->pre = p2;
            p2->next = mp;
            p2 = mp;
        }
    }
    int num=0;
    fclose(fp);             //关闭fp所指向的文件
    //printf("时     间          金   额                 收入来源或支出目的\n");
    QStandardItem *aItem;
    if (pr != NULL)
    {
        pr->next = NULL;
        mp = pr;
        while (mp != NULL)
        {
            //printf("%d.%d.%-12d%c%-24f%s\n", p->year, p->month, p->day, p->ch, p->iae, p->source_or_aim);
            aItem=new QStandardItem(QString::asprintf("%d.%d.%d",mp->year, mp->month, mp->day));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,0,aItem);
            aItem=new QStandardItem(QString::asprintf("%c%f",mp->ch, mp->iae));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,1,aItem);
            aItem=new QStandardItem(QString::asprintf("%s",mp->source_or_aim));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,2,aItem);
            i++;
            mp = mp->pre;
            num++;
        }
    }
    else if (pr == NULL)
    {
        //printf("您还没记录任何信息哦~");
        aItem=new QStandardItem(QString::asprintf("未记录任何信息"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
    }
}

void CangKu::on_action_2_triggered()//查看剩余库存
{
    QStringList headlist;
    headlist<<"产品编号"<<"产品"<<"产品形式"<<"剩余数量"<<"进价"<<"售价"<<"生产日期"<<"进货日期"<<"保质期";
    themodel=new QStandardItemModel(count1+1,9,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStandardItem *aItem;
    int num=0;
    if(count1==0){
        aItem=new QStandardItem(QString::asprintf("无剩余库存"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        return;
    }
    struct good *tmp;
    tmp=p;
    for(int i=0;i<count1;i++){
        //产品编号
        aItem=new QStandardItem(QString::asprintf("%s",tmp->number));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,0,aItem);
        //"产品"
        aItem=new QStandardItem(QString::asprintf("%s(%s)",tmp->name,tmp->nickname));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,1,aItem);
        //产品形式
        std::string selltyp;
        selltyp="盒装";
        if(tmp->typ==1) selltyp="散装";
        aItem=new QStandardItem(QString::asprintf("%s",selltyp.c_str()));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,2,aItem);
        //"剩余数量"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->count));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,3,aItem);
        //"进价"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->inprice));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,4,aItem);
        //"售价"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->outprice));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,5,aItem);
        //"进货日期"
        aItem=new QStandardItem(QString::asprintf("%s",tmp->purchasedate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,6,aItem);
        //"生产日期"
        aItem=new QStandardItem(QString::asprintf("%s",tmp->indate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,7,aItem);
        //"保质期"
        aItem=new QStandardItem(QString::asprintf("%d",tmp->exp));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,8,aItem);
        tmp=tmp->next;
    }
    tmp=NULL;
}

int CangKu::js()//进货订单数量
{
    FILE* fp;              //文件指针
    int n = 0;
    Accp p;
    fp = fopen("admin123_purchase", "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       return 0;
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {
        p=(Accp)malloc(M);//则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        fread(p, M, 1, fp);
        n++;
        free(p);
    }
    fclose(fp);
    return n;
}

int CangKu::judgegq()//判断是否过期
{
    QDateTime curti=QDateTime::currentDateTime();
    int a1=ui->yearbox->value();
    int a2=curti.toString("yy").toInt();
    int a3=ui->monthbox->value();
    int a4=curti.toString("MM").toInt();
    int a5=ui->daybox->value();
    int a6=curti.toString("dd").toInt();
    int s=day_diff(a1,a3,a5,a2,a4,a6);
    if(s>=ui->exp->value()){
        return 0;//非法
    }
    return 1;//合法
}

//读取当前剩余果蔬信息
struct good* log_start(struct good *tp,int *count19)
{
    FILE* r=fopen("shiyan4.txt","r");
    if(r==NULL){
        return NULL;
    }
    int n;
    fread(&n,sizeof(int),1,r);
    *count19=n;
    tp=(struct good*)malloc(sizeof(struct good));
    struct good *tmp;
    tmp=(struct good*)malloc(sizeof(struct good));
    tp=tmp;
    for(int i=0;i<n;i++){
        fread(tmp,sizeof(struct good),1,r);
        tmp->next=(struct good*)malloc(sizeof(struct good));
        tmp=tmp->next;
    }

    tmp=NULL;
    fclose(r);
    return tp;
}

//读取历史全部果蔬信息
struct good* log_start1(struct good *yp,int *count10)
{
    FILE* r1=fopen("shiyan5.txt","r");
    if(r1==NULL){
        return NULL;
    }
    int n;
    fread(&n,sizeof(int),1,r1);
    *count10=n;
    yp=(struct good*)malloc(sizeof(struct good));
    struct good *tmp;
    tmp=(struct good*)malloc(sizeof(struct good));
    yp=tmp;
    for(int i=0;i<n;i++){
        fread(tmp,sizeof(struct good),1,r1);
        tmp->next=(struct good*)malloc(sizeof(struct good));
        tmp=tmp->next;
    }
    tmp=NULL;
    fclose(r1);
    return yp;
}

//读取历史卖出果蔬信息
struct good* log_start2(struct good *zp,int *count11)
{
    FILE* r2=fopen("shiyan6.txt","r");
    if(r2==NULL){
        return NULL;
    }
    int n;
    fread(&n,sizeof(int),1,r2);
    *count11=n;
    zp=(struct good*)malloc(sizeof(struct good));
    struct good *tmp;
    tmp=(struct good*)malloc(sizeof(struct good));
    zp=tmp;
    for(int i=0;i<n;i++){
        fread(tmp,sizeof(struct good),1,r2);
        tmp->next=(struct good*)malloc(sizeof(struct good));
        tmp=tmp->next;
    }
    tmp=NULL;
    fclose(r2);
    return zp;
}
//读取已过期产品
struct good* log_start3(struct good *zp,int *count11)
{
    FILE* r2=fopen("shiyan7.txt","r");
    if(r2==NULL){
        return NULL;
    }
    int n;
    fread(&n,sizeof(int),1,r2);
    *count11=n;
    zp=(struct good*)malloc(sizeof(struct good));
    struct good *tmp;
    tmp=(struct good*)malloc(sizeof(struct good));
    zp=tmp;
    for(int i=0;i<n;i++){
        fread(tmp,sizeof(struct good),1,r2);
        tmp->next=(struct good*)malloc(sizeof(struct good));
        tmp=tmp->next;
    }
    tmp=NULL;
    fclose(r2);
    return zp;
}
//对订单进行字典序排序
void cmp_count(struct good *np,int *count12)
{
    struct good *p1,*q;
    double t;
    int a;
    char s1[50];
    p1=np;
    for(int i=0;i<*count12;i++)
    {
        q=p1->next;
        for(int j=i+1;j<*count12;j++)
        {
            if(strcmp(p1->name,q->name)>0){
                strcpy(s1,p1->name);
                strcpy(p1->name,q->name);
                strcpy(q->name,s1);
                strcpy(s1,p1->nickname);
                strcpy(p1->nickname,q->nickname);
                strcpy(q->nickname,s1);
                strcpy(s1,p1->number);
                strcpy(p1->number,q->number);
                strcpy(q->number,s1);
                t=p1->count;
                p1->count=q->count;
                q->count=t;
                t=p1->inprice;
                p1->inprice=q->inprice;
                q->inprice=t;
                t=p1->outprice;
                p1->outprice=q->outprice;
                q->outprice=t;
                strcpy(s1,p1->indate);
                strcpy(p1->indate,q->indate);
                strcpy(q->indate,s1);
                strcpy(s1,p1->purchasedate);
                strcpy(p1->purchasedate,q->purchasedate);
                strcpy(q->purchasedate,s1);
                a=p1->exp;
                p1->exp=q->exp;
                q->exp=a;
                a=p1->typ;
                p1->typ=q->typ;
                q->typ=a;
            }
            q=q->next;
        }
        p1=p1->next;
    }
}

//判断过期产品
void del1()
{
    QDateTime curti=QDateTime::currentDateTime();
      int i,j=0;
      struct good *tmp,*p1;
      tmp=p;
      int n=count1;
      for(i=0;i<n;i++){
          int a1=tmp->purchasedate[1]-'0'+(tmp->purchasedate[0]-'0')*10;
          int a2=curti.toString("yy").toInt();
          int a3=tmp->purchasedate[4]-'0'+(tmp->purchasedate[3]-'0')*10;
          int a4=curti.toString("MM").toInt();
          int a5=tmp->purchasedate[7]-'0'+(tmp->purchasedate[6]-'0')*10;
          int a6=curti.toString("dd").toInt();
          int s=day_diff(a1,a3,a5,a2,a4,a6);
          if(s > tmp->exp){
              if(j==0){
                  p=tmp->next;
                  //tmp=tmp->next;
              }
              else{
                  p1->next=tmp->next;
                  //tmp=tmp->next;
              }
              count1-=1;
              if(count1==0){
                  p=NULL;
                  int nxxx2=0;
                  FILE *wzz11=fopen("shiyan4.txt","w");
                  fwrite(&nxxx2,sizeof(int),1,wzz11);
                  fclose(wzz11);
                  return ;
              }else{
                  FILE *wzz1=fopen("shiyan4.txt","w");
                  if(wzz1==NULL){
                      return;
                  }
                  int nxxx=count1;
                  struct good *ppzz;
                  ppzz=p;
                  fwrite(&nxxx,sizeof(int),1,wzz1);
                  for(int i=0;i<count1;i++){
                      fwrite(ppzz,sizeof(struct good),1,wzz1);
                      ppzz=ppzz->next;
                  }
                  fclose(wzz1);
                  }
          }
          else {j++;}
          p1=tmp;
          tmp=tmp->next;
      }
}

void CangKu::on_action_3_triggered()//查看过期产品
{
    QStringList headlist;//indate guoqi
    headlist<<"产品编号"<<"产品"<<"产品形式"<<"过期数量"<<"进价"<<"过期时间";
    themodel=new QStandardItemModel(count4+1,6,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStandardItem *aItem;
    int num=0;
    if(count4==0){
        aItem=new QStandardItem(QString::asprintf("无过期产品"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        return;
    }
    struct good *tmp;
    tmp=ss3;
    for(int i=0;i<count4;i++){
        //产品编号
        aItem=new QStandardItem(QString::asprintf("%s",tmp->number));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,0,aItem);
        //"产品"
        aItem=new QStandardItem(QString::asprintf("%s(%s)",tmp->name,tmp->nickname));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,1,aItem);
        //产品形式
        std::string selltyp;
        selltyp="盒装";
        if(tmp->typ==1) selltyp="散装";
        aItem=new QStandardItem(QString::asprintf("%s",selltyp.c_str()));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,2,aItem);
        //"过期数量"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->count));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,3,aItem);
        //"进价"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->inprice));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,4,aItem);
        //过期时间
        aItem=new QStandardItem(QString::asprintf("%s",tmp->indate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,5,aItem);
        tmp=tmp->next;
    }
    tmp=NULL;
}

